<?php

abstract class CodeSpace_Jplugins_View_Helper_JpluginsAbstract extends Zend_View_Helper_Abstract
{
    
    protected function setStyleSheets()
    {

        if(array_key_exists('stylesheet', $this->_options))
        {
            if(array_key_exists('theme', $this->_options))
            {
                $dir = '/' . $this->_options['theme'] . '/';
            }
            else {
                $dir = '/';
            }
      
            if(is_array($this->_options['stylesheet']))
            {
                foreach ($this->_options['stylesheet'] as $stylesheet)
                {
                    $this->view->jquery()
                        ->addStylesheet(
                            $this->view->baseUrl('js/jquery/' . $this->_options['name'] . $dir .$stylesheet),
                            'screen'
                    );
                } 
            }               
            else 
            {
                if(isset($this->_options['stylesheet']))
                 {
                     $this->view->jquery()
                         ->addStylesheet(
                             $this->view->baseUrl('js/jquery/' . $this->_options['name'] . $dir .$this->_options['stylesheet']),
                         	'screen'
                     );
                 }
             }
        }             
    }
    
    protected function setJsFiles()
    {

        if(array_key_exists('jsFile', $this->_options))
        {
            if(is_array($this->_options['jsFile']))
            {
                foreach ($this->_options['jsFile'] as $jsFile)
                {
                    $this->view->jquery()
                        ->addJavascriptFile(
                            $this->view->baseUrl('js/jquery/' . $this->_options['name'] . '/' .$jsFile)
                    );
                }                
            }
            else 
            {
                if(isset($this->_options['stylesheet']))
                 {
                    $this->view->jquery()
                        ->addJavascriptFile(
                            $this->view->baseUrl('js/jquery/' . $this->_options['name'] . '/' .$this->_options['jsFile'])
                    );
                }
            }
        }
    }   
    
    protected function loadOptions()
    {
        if(isset($this->_options['loader'])){
  		    $this->view->jquery()->onLoadCaptureStart();	
   			    echo $this->parser('js/jquery/'. $this->_options['name'] . '/' . $this->_options['loader'], $this->_options);
   		    $this->view->jquery()->onLoadCaptureEnd();   
        }     
    }
    
    protected function loadContent($params = null)
    {
        if(isset($this->_options['content'])){
            if($params === null){
                include_once 'js/jquery/'. $this->_options['name'] . '/' . $this->_options['content'];
            }else {
                return $this->parser('js/jquery/'. $this->_options['name'] . '/' . $this->_options['content'], $params);
            }
        }
    }
    
    protected function parser($file, $params)
    {
    	$content = file_get_contents($file);
        preg_match_all('/\{php:(.*?)\}/', $content, $var_list);
        $var_list = $var_list[0];
        foreach ($var_list as $value) {
        	$var_key = preg_replace('/\{php:(.*?)\}/', '$1', $value);
        	if(array_key_exists('option', $params)){
        	    $content = str_replace('{php:' . $var_key . '}', $var_key . ': ' . $params['option'][$var_key] , $content);    
        	}else {
        	    $content = str_replace('{php:' . $var_key . '}', $params[$var_key] , $content);
        	}
        }

        return $content;
    }

}
